<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\CountryModel;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function country(Request $request)
    {
        $countries = CountryModel::get();
        return view("admin.country", compact("countries"));
    }

    public function countryaddpage(Request $request)
    {
        $id = $request->input("id");
        $country = CountryModel::find($id);
        return view("admin.countryaddpage", compact("country"));
    }

    public function countryaddinsert
    (Request $request)
    {
        $data = $request->except("_token");
        if ($data['id'] == 0) {
            CountryModel::create($data);
            $result = ["title"=>"添加数据成功！","err"=>0];
        } else {
            $country = CountryModel::find($data['id']);
            $country->update($data);
            $result = ["title"=>"数据编辑成功！","err"=>0];
        }
        $result = json_encode($result);
        return $result;
    }

    public function countrydelete(Request $request)
    {
        $id = $request->input("id");
        CountryModel::where("id",$id)->delete();
        $result = ["title"=>"删除成功！","err"=>0];
        $result = json_encode($result);
        return $result;
    }
}
